package com.guangjian.asc.retail.web;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.guangjian.asc.common.web.BaseController;
import com.guangjian.asc.retail.domain.OrderTemplateItem;

@RooWebScaffold(path = "order-template-item", formBackingObject = OrderTemplateItem.class)
@RequestMapping("/order-template-item")
@Controller
public class OrderTemplateItemController extends BaseController {

	@RequestMapping(method = RequestMethod.POST)
	public String create(@Valid OrderTemplateItem orderTemplateItem, BindingResult result, Model model,
			HttpServletRequest request) {
		if (result.hasErrors()) {
			model.addAttribute("orderTemplateItem", orderTemplateItem);
			return "order-template-item/create";
		}

		if (orderTemplateItem.getSalePrice() == null) {
			orderTemplateItem.setSalePrice(orderTemplateItem.getProduct().getDefaultPrice());
		}

		orderTemplateItem.persist();
		return "redirect:/order-template-item/" + encodeUrlSegment(orderTemplateItem.getId().toString(), request);
	}
}
